<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 示例接口
 */
class Bjl extends Api
{

    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //http://bjls.test/api/bjl/gethandicaplist
    public function gethandicaplist()
    {
        $list = \app\admin\model\Handicap::all();

        $this->success('返回成功', $list);
    }

    //http://bjls.test/api/bjl/postagent
    //新增代理商自动生成dd_user和fa_admin
    public function postagent()
    {
        $pid = $this->request->post('pid');//父代理商ID
        $wjzh = $this->request->post('wjzh');//帳號
        $bzmc = $this->request->post('bzmc');//昵称
        $mima = $this->request->post('mima');//密碼
        $bzinfo = $this->request->post('bzinfo');//备注
        $game1_proportion = $this->request->post('game1_proportion');
        $game1_refund = $this->request->post('game1_refund');
        $game2_proportion = $this->request->post('game2_proportion');
        $game2_refund = $this->request->post('game2_refund');
        $handicap = $this->request->post('handicap');
        $czmima = $this->request->post('czmima');
        $id = $this->request->post('id', 0);//编辑代理商ID

        $id = intval($id);

        $dd_user_id = 0;
        $fagent = \app\admin\model\Agent::get(intval($pid));
        if (!$fagent) {
            $this->error("父代理商错误！");
        }

        if ($id == 0) {
            $user = \app\admin\model\dd\User::where("login_name", $wjzh)->find();//账号不能重复
            if ($user) {
                $this->error("已有重复的账号！");
            }
            $user = new \app\admin\model\dd\User();
            $user->login_name = $wjzh;
            $user->login_pass = md5($mima);
            $user->mima = $mima;
            $user->nickname = $bzmc;
            $user->agent_pid = intval($pid);
            $user->game1_refund = $game1_refund;
            $user->game2_refund = $game2_refund;
            $user->sub_time = time();
            $user->type = 3;
            $user->handicap = $handicap;
            $user->czmima = $czmima;
            $user->remark = $bzinfo;
            $user->save();

            $faadmin = new \app\admin\model\Admin();
            $faadmin->username = $wjzh;
            $faadmin->nickname = $bzmc;
            $faadmin->salt = "Colin";
            $faadmin->email = $wjzh."@qq.com";
            $faadmin->password = $this->auth->getEncryptPassword($mima, $faadmin->salt);
            $faadmin->avatar = '/assets/img/avatar.png';
            $faadmin->save();

            $aga = new \app\admin\model\AuthGroupAccess();
            $aga->uid = $faadmin->id;
            $aga->group_id = 9;
            $aga->save();

            $agent = new \app\admin\model\Agent();
            $agent->dd_user_id = $user->id;
            $agent->admin_id = $faadmin->id;

            $agent->level = $fagent->level + 1;
            $agent->name = $bzmc;
            $agent->pid = intval($pid);
            $agent->game1_refund = $game1_refund;
            $agent->game2_refund = $game2_refund;
            $agent->game1_proportion = $game1_proportion;
            $agent->game2_proportion = $game2_proportion;
            $agent->handicap = $handicap;
            $agent->save();

            $dd_user_id = $agent->dd_user_id;
            
        } else {
            $agent = \app\admin\model\Agent::get($id);
            if (!$agent) {
                $this->error("代理商不存在！");
            }

            $user = \app\admin\model\dd\User::get($agent->dd_user_id);
            if (!$user) {
                $this->error("dd_user不存在！");
            }
            $user->login_name = $wjzh;
            $user->login_pass = md5($mima);
            $user->mima = $mima;
            $user->nickname = $bzmc;
            $user->agent_pid = intval($pid);
            $user->game1_refund = $game1_refund;
            $user->game2_refund = $game2_refund;
            $user->sub_time = time();
            $user->type = 3;
            $user->handicap = $handicap;
            $user->czmima = $czmima;
            $user->remark = $bzinfo;
            $user->save();

            $faadmin = \app\admin\model\Admin::get($agent->admin_id);
            if (!$faadmin) {
                $this->error("dd_user不存在！");
            }
            $faadmin->username = $wjzh;
            $faadmin->nickname = $bzmc;
            $faadmin->salt = "Colin";
            $faadmin->email = $wjzh."@qq.com";
            $faadmin->password = $this->auth->getEncryptPassword($mima, $faadmin->salt);
            $faadmin->avatar = '/assets/img/avatar.png';
            $faadmin->save();

            $agent->level = $fagent->level + 1;
            $agent->name = $bzmc;
            $agent->pid = intval($pid);
            $agent->game1_refund = $game1_refund;
            $agent->game2_refund = $game2_refund;
            $agent->game1_proportion = $game1_proportion;
            $agent->game2_proportion = $game2_proportion;
            $agent->handicap = $handicap;
            $agent->save();

            $dd_user_id = $agent->dd_user_id;
        }


        $this->success('返回成功', ['id' => $dd_user_id]);
    }

    //http://bjls.test/api/bjl/postagent
    //新增dd_user
    public function postuser()
    {
        $pid = $this->request->post('pid');//父代理商ID
        $wjzh = $this->request->post('wjzh');//帳號
        $bzmc = $this->request->post('bzmc');//昵称
        $mima = $this->request->post('mima');//密碼
        $bzinfo = $this->request->post('bzinfo');//备注
        $game1_limit = $this->request->post('game1_limit');
        $game1_refund = $this->request->post('game1_refund');
        $game2_limit = $this->request->post('game2_limit');
        $game2_refund = $this->request->post('game2_refund');
        $handicap = $this->request->post('handicap');
        $czmima = $this->request->post('czmima');

        $fagent = \app\admin\model\Agent::get(intval($pid));
        if (!$fagent) {
            $this->error("父代理商错误！");
        }

        $user = \app\admin\model\dd\User::where("login_name", $wjzh)->find();//账号不能重复
        if ($user) {
            $this->error("已有重复的账号！");
        }

        $user = new \app\admin\model\dd\User();
        $user->login_name = $wjzh;
        $user->login_pass = md5($mima);
        $user->mima = $mima;
        $user->nickname = $bzmc;
        $user->agent_pid = intval($pid);
        $user->game1_refund = $game1_refund;
        $user->game2_refund = $game2_refund;
        $user->game1_limit = $game1_limit;
        $user->game2_limit = $game2_limit;
        $user->sub_time = time();
        $user->type = 3;
        $user->handicap = $handicap;
        $user->czmima = $czmima;
        $user->remark = $bzinfo;
        $user->save();

        $faadmin = new \app\admin\model\Admin();
        $faadmin->username = $wjzh;
        $faadmin->nickname = $bzmc;
        $faadmin->salt = "Colin";
        $faadmin->email = $wjzh."@qq.com";
        $faadmin->password = $this->auth->getEncryptPassword($mima, $faadmin->salt);
        $faadmin->avatar = '/assets/img/avatar.png';
        $faadmin->save();

        $aga = new \app\admin\model\AuthGroupAccess();
        $aga->uid = $faadmin->id;
        $aga->group_id = 9;
        $aga->save();

        $this->success('返回成功', ['id' => $user->id]);
    }

    //http://bjls.test/api/bjl/postedfp
    //玩家玩遊戲的額度
    public function postedfp()
    {
        $id = $this->request->post('id');//父代理商ID
        $cred = $this->request->post('cred');//money

        $dduser = \app\admin\model\dd\User::get(intval($id));
        if (!$dduser) {
            $this->error("用户ID错误！");
        }
        $dduser->money = floatval($cred);
        $dduser->save();

        $this->success('返回成功');
    }

}
